package com.wondersgroup.retrofittest.mall.presenter;
import com.hss.mvp.AbstractPresenter;
import com.hss.rxjava.ApiCallback;
import com.hss.rxjava.bean.GoodsBean;
import com.hss.rxjava.result.ResultListObject;
import com.wondersgroup.retrofittest.mall.contract.MallContract;
import com.wondersgroup.retrofittest.mall.model.MallModelImpl;

import rx.Observable;

/**
* Created by Administrator on 2017/02/10
*/

public class MallPresenterImpl extends AbstractPresenter implements MallContract.Presenter{
    MallContract.View mvpView;
    MallContract.Model mvpModel;
    int isDrop = 0;
    public MallPresenterImpl() {
        mvpModel = new MallModelImpl();
    }

    @Override
    public void attachView(MallContract.View mvpView) {
        this.mvpView = mvpView;
    }

    @Override
    public void detachView() {
        this.mvpView=null;
        onUnsubscribe();
    }

    @Override
    public void destoryRes() {

        mvpModel = null;

    }

    @Override
    public void loadGoods(final int isDrop, int page, int pageCount) {
        this.isDrop = isDrop;
        if (isDrop == 0){
            mvpView.showMSVLoading();
        }
        addSubscription((Observable) mvpModel.loadGoods(page, pageCount), new GoodsListCallBack());
    }

    class GoodsListCallBack extends ApiCallback<ResultListObject<GoodsBean>>{
        @Override
        public void onSuccess(ResultListObject<GoodsBean> result) {
            if (MallPresenterImpl.this.isDrop !=2){
                if (isDrop == 1){
                    mvpView.refreshComplete();
                }

                if (result.getResponse() == null || result.getResponse().size() == 0){
                    mvpView.showMSVEmpty();
                }else {

                    mvpView.refreshData(true,result.getResponse());
                    //                        if (MallPresenterImpl.this.isDrop == 0){
                    mvpView.showMSVContent();
                    if (result.getResponse().size() == 10){
                        mvpView.loadMoreEnable(true);
                    }else {
                        mvpView.loadMoreEnable(false);
                    }
//                        }
                }
            }else{
                mvpView.loadMoreComplete();
                if (result.getResponse() == null || result.getResponse().size() == 0){
                    mvpView.showToast(" no more data");
                }else {
                    mvpView.refreshData(false,result.getResponse());
                    if (result.getResponse().size() == 10){
                        mvpView.loadMoreEnable(true);
                    }else {
                        mvpView.loadMoreEnable(false);
                    }
                }
            }
        }

        @Override
        public void onFailure(String msg) {
            if (MallPresenterImpl.this.isDrop !=2){
                mvpView.showMSVUnknowErr();
            }else{
                mvpView.showToast(msg);
            }
        }

        @Override
        public void onFinish() {

        }

        @Override
        public void onDataErr(String msg) {
            if (MallPresenterImpl.this.isDrop !=2){
                mvpView.showMSVUnknowErr();
            }else{
                mvpView.showToast(msg);
            }
        }

        @Override
        public void onExitLogin() {

        }
    };
}